//
//  ANVIPCartInfoController.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/13.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANVIPCartInfoController.h"
#import "inputCell.h"
#import "VIPCartNumView.h"
#import "MineInfoFooterView.h"
#import "WXApiManager.h"

@interface ANVIPCartInfoController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) VIPCartNumView *headerView;
@property (nonatomic, strong) MineInfoFooterView *footerView;
@end

@implementation ANVIPCartInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"会员信息";
    
    WeakSelf
    [WXApiManager sharedManager].resultHandle = ^(NSString * _Nullable orderId) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
}

- (void)setupSubViews
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.headerView = [[VIPCartNumView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.headerView.cartNum = self.userInfoEntity.orgCode;
    self.tableView.tableHeaderView = self.headerView;
    
    if (self.userInfoEntity.remainNum.intValue > 0) {
        WeakSelf
        self.footerView = [[MineInfoFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, kWidth(100))];
        self.footerView.title = @"立即还款";
        self.tableView.tableFooterView = self.footerView;
        self.footerView.sureHandler = ^{
            // 还款
            [weakSelf showPayType];
        };
    }
    
}

- (void)requestDataWithType:(int)type payWay:(int)payWay
{
    NSString *amount;
    if (type == 1) {
        amount = self.userInfoEntity.remainAmount;
    }
    else {
        amount = self.userInfoEntity.perAmount;
    }
    NSDictionary *body = @{
                           @"memberId":UserUtils.shared.vipId,
                           @"type":@(type),//1全部还款 2还一期
                           @"payWay":@(payWay),//1微信 2支付宝
                           @"amount":amount
                           };
    [MBProgressHUD showActivityMessage:@"请求中..."];
    [URLManager.shareManager postUrlStr:Api_Repayment parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                NSDictionary *data = responseObj[@"data"];
                NSString *transRecordId = data[@"transRecordId"];
                if (payWay == 1) {//微信
                    [[WXApiManager sharedManager] wxPayWithOrderId:nil transRecordId:transRecordId];
                }
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

- (void)showPayType
{
    LLAlertViewController *vc = [[LLAlertViewController alloc] init];
    vc.actions = @[@"立即还款",@"全额还款"];
    vc.actionBlock = ^(NSInteger index, NSString *actionTitle) {
        if (index == 0) {//还款
            [self showPaymentWithType:2];
        }
        else {//全额还款
            [self showPaymentWithType:1];
        }
    };
    [vc presentBottomViewControllerFromController:self];
}

- (void)showPaymentWithType:(int)type
{
    LLAlertViewController *vc = [[LLAlertViewController alloc] init];
//    vc.actions = @[@"微信支付",@"支付宝"];
//    vc.actionImgs = @[@"pay_wechat",@"pay_alipay"];
    vc.actions = @[@"微信支付"];
    vc.actionImgs = @[@"pay_wechat"];
    vc.actionBlock = ^(NSInteger index, NSString *actionTitle) {
        if (index == 0) {//微信
            [self requestDataWithType:type payWay:1];
        }
        else {//支付宝
            [self requestDataWithType:type payWay:2];
        }
    };
    [vc presentBottomViewControllerFromController:self];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.userInfoEntity.remainNum.intValue > 0) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    inputCell *cell = [tableView dequeueReusableInputCellWithIdentifier:@"inputCell"];
    cell.editable = NO;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"会员套餐";
            cell.text = self.userInfoEntity.typeName;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"套餐价格";
            cell.text = [NSString stringWithFormat:@"￥%@",StringNone(self.userInfoEntity.price)];
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"所属商户";
            cell.text = self.userInfoEntity.consumerName;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"最近还款时间";
            cell.text = self.userInfoEntity.recentRepayTime;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"本期还款金额";
            cell.text = [NSString stringWithFormat:@"￥%@",StringNone(self.userInfoEntity.perAmount)];
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"剩余还款期数";
            cell.text = self.userInfoEntity.remainNum;
        }
        if (indexPath.row == 3) {
            cell.titleLab.text = @"剩余还款总金额";
            cell.text = [NSString stringWithFormat:@"￥%@",StringNone(self.userInfoEntity.remainAmount)];
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 50;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
